package com.zhao.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.event.SyncReadListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhao.entity.EduSubject;
import com.zhao.entity.execl.SubjectExecl;
import com.zhao.entity.subject.OneSubject;
import com.zhao.entity.subject.TwoSubject;
import com.zhao.listen.SubjectListen;
import com.zhao.mapper.EduSubjectMapper;
import com.zhao.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-07-01
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file, EduSubjectService subjectService) {

        try {
            //获取字符流
            InputStream in=file.getInputStream();
            //读取execl表格中的内容
            EasyExcel.read(in,SubjectExecl.class,new SubjectListen(subjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public List<OneSubject> selectAll() {
        //第一步：获取所有的一级分类
        QueryWrapper<EduSubject>wrapper=new QueryWrapper<>();
        wrapper.eq("parent_id",0);
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapper);
        //第二步：获取所有的二级分类
        QueryWrapper<EduSubject>wrapper1=new QueryWrapper<>();
        //ne表示不等于
        wrapper1.ne("parent_id",0);
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapper1);
        //创建一个最终的结果来封装一级分类
        List<OneSubject>finalSubjectList=new ArrayList<>();
        //第三步：封装一级分类
        //遍历一级分类
        oneSubjectList.forEach(subject->{
            OneSubject oneSubject=new OneSubject();
            oneSubject.setId(subject.getId());
            oneSubject.setTitle(subject.getTitle());
            //创建二级分类集合来添加二级分类
            List<TwoSubject>tsubject=new ArrayList<>();
            //第四步：遍历二级分类
            twoSubjectList.forEach(twoSubject->{
                TwoSubject Twosubjects=new TwoSubject();
                Twosubjects.setId(twoSubject.getId());
                Twosubjects.setTitle(twoSubject.getTitle());
                //只有当一级分类的id等于二级分类的parent_id时才可以把二级分类添加进二级集合中
                if(subject.getId().equals(twoSubject.getParentId())){
                    //把二级分类添加进二级分类集合中
                    tsubject.add(Twosubjects);
                }
                //把二级分类的集合添加进一级分类
                oneSubject.setChildren(tsubject);
            });
            //把一级分类添加进最总的结过中
            finalSubjectList.add(oneSubject);
        });

        return finalSubjectList;
    }
}
